package com.shujia.hadoop.mr;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;


public class MR01 {
    public static class WordMapper extends Mapper<LongWritable,Text,Text,LongWritable >{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line=value.toString ();
            int v=1;
            context.write (new Text (line),new LongWritable (v));
        }
    }
    public static class WordReduce extends Reducer<Text,LongWritable,Text,LongWritable >{
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            int count=0;
            for (LongWritable value : values) {
                count+=value.get ();
            }
            context.write (key,new LongWritable(count));
        }
    }

    public static void main(String[] args)throws Exception {
        Job job = Job.getInstance ( );
        job.setJobName ("第一个mr程序，单词统计");
        job.setJarByClass (MR01.class);
        job.setNumReduceTasks (2);
        job.setMapperClass (WordMapper.class);
        job.setReducerClass (WordReduce.class);
        job.setMapOutputKeyClass (Text.class);
        job.setMapOutputValueClass (LongWritable.class);
        job.setOutputKeyClass (Text.class);

        job.setOutputValueClass (LongWritable.class);
        Path input = new Path ("/words.txt");
        Path out = new Path ("/output/data");
        FileInputFormat.addInputPath (job,input);
        FileOutputFormat.setOutputPath(job,out);
        job.waitForCompletion (true);
        System.out.println ("正在运行mr" );

    }

}